<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    >
      <template #btns>
        <el-button :loading="exLoading" type="primary" @click="handleEx">{{
          translate("export")
        }}</el-button>
      </template>
    </QueryForm>
    <div class="mb20">
      <el-button type="primary" @click="resetSend">{{
        translate("resetSend")
      }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        v-for="item in propList"
        :key="item.label"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :min-width="item.minWidth"
        show-overflow-tooltip
      />
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <el-dialog
      :title="translate('detail')"
      :visible.sync="isShowDetail"
      width="600px"
    >
      <el-form ref="form" label-width="auto">
        <el-form-item :label="translate('title')">
          <el-input v-model="detail.title" disabled />
        </el-form-item>
        <el-form-item :label="translate('content')">
          <el-input v-model="detail.title" disabled />
        </el-form-item>
        <el-form-item :label="translate('pubTime')">
          <el-input v-model="detail.title" disabled />
        </el-form-item>
        <el-form-item :label="translate('attachment')">
          <el-input v-model="detail.title" disabled />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowDetail = false">{{
          translate("cancel")
        }}</el-button>
        <el-button type="primary" @click="isShowDetail = false">{{
          translate("enter")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { smsSendRecord, smsSendRecordEx, venueList, smsReSend } from '@/api'
import { propList, queryFormConfig } from './config'
export default {
  data() {
    return {
      propList,
      queryFormConfig,
      listLoading: false,
      exLoading: false,
      list: [],
      selList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},
      detail: {},
      isShowDetail: false
    }
  },
  created() {
    this.getList(1)
    this.getVenueList()
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    resetSend() {
      const { selList } = this
      if (selList.length === 0) return this.$message.warning(this.translate('qxzjl'))
      smsReSend({ param: { ids: selList.map(i => i.id) }}).then(res => {
        if (res.errorCode !== '000000') return
        this.$message.success(this.translate('sysresetScs'))
        this.getList()
      })
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          arr.forEach(e => {
            e.label = e.name
            e.value = e.id
          })
          this.queryFormConfig.formItems[0].options = arr
        })
    },
    handleDetail(row) {
      this.isShowDetail = true
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (param.selTime && param.selTime.length > 0) {
        param.startTime = param.selTime[0]
        param.endTime = param.selTime[1]
      } else {
        param.startTime = null
        param.endTime = null
      }
      this.listLoading = true
      smsSendRecord({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList.map(item => {
            item.sendStatusName = item.sendStatus === '0' ? this.translate('success') : this.translate('error')
            return item
          })
          this.total = res.totalCount
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleSelectionChange(val) {
      this.selList = val
    },
    handleEx() {
      const { filters: param } = this
      if (param.selTime && param.selTime.length > 0) {
        param.startTime = param.selTime[0]
        param.endTime = param.selTime[1]
      } else {
        param.startTime = null
        param.endTime = null
      }
      this.exLoading = true
      smsSendRecordEx({ param: { ...param, shopId: localStorage.getItem('shopId') }}).then(res => {
        this.exLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => {
        this.exLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style>
</style>
